local skel = fk.CreateSkill{
  name = "lb__xuanye",
}

Fk:loadTranslationTable{
  ["lb__xuanye"] = "喧夜",
  --[":lb__xuanye"] = "结束阶段，你可以令一名角色使用其拥有的「<b>丝</b>」。拥有「<b>丝</b>」的角色受到属性伤害后，你可令拥有「<b>丝</b>」的所有角色均受到此伤害并重铸「<b>丝</b>」。",
  --10.6
  [":lb__xuanye"] = "结束阶段，你可以令一名角色对你指定的一名角色使用其拥有的「<b>丝</b>」。拥有「<b>丝</b>」的角色受到属性伤害后，你可令拥有「<b>丝</b>」的所有角色均受到此伤害并重铸「<b>丝</b>」。",

  ["#lb__xuanye"] = "喧夜：你可以令一名角色对你指定的一名角色使用其拥有的「<b>丝</b>」",
  ["#lb__xuanye_ask"] = "喧夜：令%dest对你指定的一名角色使用其拥有的「<b>丝</b>」",
  ["#lb__xuanye_use"] = "喧夜：请使用一张「<b>丝</b>」",
  ["#lb__xuanye_damage"] = "喧夜：你可以令拥有「<b>丝</b>」的所有角色均受到 %arg点%arg2伤害 并重铸「<b>丝</b>」",

  ["#lb__xuanye_ChainDamage"] = "%from 拥有「<b>丝</b>」，将受到传导伤害并重铸「<b>丝</b>」",

  ["$lb__xuanye1"] = "该说再见了。",
  ["$lb__xuanye2"] = "美妙的时光总有尽头。",
  ["$lb__xuanye3"] = "躯体皆因脆弱而美丽。",
}

skel:addEffect(fk.EventPhaseEnd, {
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(skel.name) and target == player and player.phase == Player.Finish
    and table.find(player.room.alive_players, function (to)
      return table.find(to:getCardIds("h"), function (id)
          return Fk:getCardById(id):getMark("@@lb__mosuo-inhand") > 0
        end) ~= nil
      end)
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local tos = table.filter(player.room.alive_players, function (to)
      return table.find(to:getCardIds("h"), function (id)
          return Fk:getCardById(id):getMark("@@lb__mosuo-inhand") > 0
        end) ~= nil
      end)
    if #tos > 0 then
      local ts = room:askToChoosePlayers(player, {
        targets = tos,
        min_num = 1,
        max_num = 1,
        skill_name = skel.name,
        prompt = "#lb__xuanye",
        cancelable = true,
      })
      if #ts > 0 then
        event:setCostData(self, {tos = ts})
        return true
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = event:getCostData(self).tos[1]
    local too = room:askToChoosePlayers(player, {
        targets = room:getAlivePlayers(),
        min_num = 1,
        max_num = 1,
        skill_name = skel.name,
        prompt = "#lb__xuanye_ask::"..to.id,
        cancelable = false,
      })[1]
    local cards = table.filter(to:getCardIds("h"), function (id)
      return Fk:getCardById(id):getMark("@@lb__mosuo-inhand") > 0
    end)
    while #cards > 0 do
      Fk.currentResponseReason = skel.name
      local use = room:askToUseRealCard(to, {
        pattern = tostring(Exppattern{ id = cards}),
        skill_name = skel.name,
        prompt = "#lb__xuanye_use",
        cancelable = false,
        skip = true,
        extra_data = {must_targets = {too.id}}
      })
      Fk.currentResponseReason = nil
      if use then
        room:useCard(use)
        cards = table.filter(to:getCardIds("h"), function (id)
          return Fk:getCardById(id):getMark("@@lb__mosuo-inhand") > 0
        end)
      else
        break
      end
    end
  end,
})

skel:addEffect(fk.Damaged, {
  can_refresh = function(self, event, target, player, data)
    return target == player and table.find(target:getCardIds("h"), function (id)
      return Fk:getCardById(id):getMark("@@lb__mosuo-inhand") > 0
    end) and not data.isVirtualDMG and Fk:canChain(data.damageType)
    --and (not data.extra_data or not data.extra_data.lb__xuanye_si)
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    data.extra_data = data.extra_data or {}
    if data.extra_data.lb__xuanye_si and type(data.extra_data.lb__xuanye_si) == "table" then
      local ids = table.filter(target:getCardIds("h"), function (id)
        return table.contains(data.extra_data.lb__xuanye_si, id)
      end)
      if #ids > 0 then
        room:recastCard(ids, target, skel.name)
      end
      return
    end
    local parent = room.logic:getCurrentEvent()--:findParent(GameEvent.Damage, true)
    if parent then
      parent:addExitFunc(function ()
        for _, to in ipairs(room:getAlivePlayers()) do
          if to:hasSkill(skel.name) then
            local tos = table.filter(player.room:getAlivePlayers(), function (t)
              return table.find(t:getCardIds("h"), function (id)
                  return Fk:getCardById(id):getMark("@@lb__mosuo-inhand") > 0
                end) ~= nil
              end)
            if #tos > 0 and room:askToSkillInvoke(to, {skill_name = skel.name, prompt = "#lb__xuanye_damage:::"..data.damage..":"..Fk:getDamageNatureName(data.damageType)}) then
              for _, si_to in ipairs(tos) do
                if si_to:isAlive() then
                  local si_ids = table.filter(si_to:getCardIds("h"), function (id)
                    return Fk:getCardById(id):getMark("@@lb__mosuo-inhand") > 0
                  end)
                  if #si_ids > 0 then
                    room:sendLog{
                      type = "#lb__xuanye_ChainDamage",
                      from = si_to.id
                    }
                    
                    local dmg = {
                      from = data.from,
                      to = si_to,
                      damage = data.damage,
                      damageType = data.damageType,
                      --card = data.card,--避免加伤重复
                      skillName = data.skillName,
                      extra_data = {lb__xuanye_si = si_ids},
                    }

                    room:damage(dmg)
                  end
                end
              end
            end
          end
        end
      end)
    end
  end,
})


return skel